package com.define.ssh.action.admin;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;

import com.define.ssh.listener.SessionListener;
import com.define.ssh.model.Func;
import com.define.ssh.model.OnlineSession;
import com.define.ssh.model.UserInfo;
import com.define.ssh.service.FuncService;
import com.define.ssh.service.UserInfoService;
import com.define.ssh.util.SystemUtil;

@ParentPackage("admin")
public class LoginAction extends BaseAdminAction {
	private static final long serialVersionUID = 1L;

	@Resource(name = "userInfoServiceImpl")
	private UserInfoService userInfoService;
	@Resource(name = "funcServiceImpl")
	private FuncService funcService;

	/**
	 * 用户登录验证
	 * 
	 * @return
	 */
	public String checkLogin() {
		try {
			final String login = this.getRequest().getParameter("login");
			final String pwd = this.getRequest().getParameter("pwd");
			final String code = this.getRequest().getParameter("code");
			if (login == null || pwd == null) {
				return ajax(Status.error, "请输入用户名称或密码");
			}
			if (code.equals("")) {
				return ajax(Status.error, "请输入验证码");
			}

			if ("".equals(login.replace(" ", ""))) {
				return ajax(Status.error, "请输入用户名称");
			} else {
				if ("".equals(pwd.replace(" ", ""))) {
					return ajax(Status.error, "请输入用户密码");
				} else {
					final String loginCode = (String) this.getRequest()
							.getSession().getAttribute("LoginCode");
					if (!code.equals(loginCode)) {
						return ajax(Status.error, "验证码有误，请重新输入");
					}
					final UserInfo user = userInfoService
							.getUserInfoByLogin(login);
					if (user == null) {
						return ajax(Status.error, "登录失败,原因：该账号不存在");
					} else {
						// pwd = Md5.md5to32(pwd.replace(" ",
						// "")).toUpperCase();
						if (pwd.equals(user.getPassword())) {
							if (user.getForbidden() == 1) {
								return ajax(Status.error, "登录失败,原因：账号禁用");
							} else {
								if (user.getRole() != null) {
									if (user.getRole().getIsvalid() == 0) {
										if (getRequest().getSession()
												.getAttribute(
														SystemUtil.USERINFO) == null) {
											super.getRequest()
													.getSession()
													.setAttribute(
															SystemUtil.USERINFO,
															user);
											super.getRequest()
													.getSession()
													.setAttribute(
															SystemUtil.USERLOGIN,
															user.getName());

											// 获取权限
											// List<Func> funcList =
											// funcService.getFuncByRoleId(user.getRole().getId());
											List<Func> funcList = funcService
													.getFuncByUserId(user
															.getId());
											super.getRequest()
													.getSession()
													.setAttribute(
															SystemUtil.FUNCTIONlIST,
															funcList);

											// 获取权限对应的根权限的菜单
											// funcList =
											// funcService.getfuncRoot(user.getRole().getId());
											funcList = funcService
													.getfuncRootByUserId(user
															.getId());
											super.getRequest()
													.getSession()
													.setAttribute(
															SystemUtil.FUNCTIONROOTlIST,
															funcList);
										}
										this.saveLog("系统登录", "登录成功");
										getRequest()
												.getSession()
												.setAttribute(
														SessionListener.LISTENER_NAME,
														new OnlineSession(
																user,
																getRequest()
																		.getRemoteAddr()));// 记录全局
										return ajax(Status.success, "登录成功");
									} else {
										return ajax(Status.error,
												"登录失败,原因：所在的用户组处于无效状态");
									}
								} else {
									return ajax(Status.error,
											"登录失败,原因：所在的用户组不存在");
								}
							}
						} else {
							return ajax(Status.error, "登录失败,原因：密码错误");
						}
					}
				}
			}
		} catch (final Exception e) {
			e.printStackTrace();
			return ajax(Status.error, "系统异常");
		}
	}

	/**
	 * 后台--首页
	 * 
	 * @return
	 * @throws Exception
	 */
	public String index() throws Exception {
		return "index";
	}

}
